package co.yixiang.controller.manager.monitoring;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.modules.monitoring.entity.domain.YxMonitoring;
import co.yixiang.modules.monitoring.entity.query.YxMonitoringQueryCriteria;
import co.yixiang.modules.monitoring.service.YxMonitoringService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "监控:设备管理")
@RestController
@RequestMapping("api/monitoring")
public class MonitoringController {

    @Autowired
    private YxMonitoringService monitoringService;

    @ApiOperation(value = "分页查询查询")
    @GetMapping
    @PreAuthorize("hasAnyRole('admin','common','MONITORING_ALL','MONITORING_SELECT')")
    public ResponseEntity getYxMonitoringPage(YxMonitoringQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity(monitoringService.queryAll(criteria, pageable), HttpStatus.OK);
    }
    @ApiOperation(value = "测试连接")
    @PostMapping(value = "/testConn")
    @PreAuthorize("hasAnyRole('admin','common','MONITORING_ALL','MONITORING_SELECT')")
    public ResponseEntity testConn(@Validated @RequestBody YxMonitoring resources) {
        // TODO 测试能不能连接上设备
        return new ResponseEntity(monitoringService.save(resources), HttpStatus.CREATED);
    }

    @ApiOperation(value = "新增")
    @PostMapping
    @PreAuthorize("hasAnyRole('admin','common','MONITORING_ALL','MONITORING_CREATE')")
    public ResponseEntity create(@Validated @RequestBody YxMonitoring resources, Boolean flag) {
        if (ObjectUtil.isEmpty(flag)) {
            flag = Boolean.FALSE;
        }
        // 增加，萤石云接口。
        return new ResponseEntity(monitoringService.saveMonitoring(resources, flag), HttpStatus.CREATED);
    }


    @ApiOperation(value = "修改")
    @PutMapping
    @PreAuthorize("hasAnyRole('admin','common','MONITORING_ALL','MONITORING_EDIT')")
    public ResponseEntity update(@Validated @RequestBody YxMonitoring resources) {
        monitoringService.updateMonitoring(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }


    @ApiOperation(value = "删除")
    @DeleteMapping
    @PreAuthorize("hasAnyRole('admin','common','MONITORING_ALL','MONITORING_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        monitoringService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }
}
